package leetcode.code1813;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution2 implements IDebug {

	public boolean areSentencesSimilar(String sentence1, String sentence2) {
		int l = 0;
		String[] words1 = sentence1.split(" ");
		String[] words2 = sentence2.split(" ");
		int len1 = words1.length;
		int len2 = words2.length;
		while (l < len1 && l < len2 && words1[l].equals(words2[l])) {
			l++;
		}
		if (l == len1 || l == len2) {
			return true;
		}
		int r1 = len1, r2 = len2;
		while (r1 > 0 && r2 > 0 && words1[r1 - 1].equals(words2[r2 - 1])) {
			r1--;
			r2--;
		}
		if (r1 == -1 || r2 == -1) {
			return true;
		}
		return l >= r1 || l >= r2;
	}

	@Override
	public void debug4() {
		H.compare(true, this.areSentencesSimilar("My name is Haley", "My Haley"));
		H.compare(true, this.areSentencesSimilar("My name is Haley", "My name"));
		H.compare(true, this.areSentencesSimilar("My name is Haley", "is Haley"));
		H.compare(false, this.areSentencesSimilar("My name is Haley", "My is"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
